<?php

namespace App\Http\Controllers\Backend;

use App\Models\Admin;

class AdminController extends BaseController
{
    // 管理员管理
    public function initialize()
    {
        parent::initialize();

        $this->model = 'admin';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作


    // 返回列表数据
    public function lists()
    {
        $where   = [['status','>=',0]];
        $keyword = $this->getData('keyword', null, 'trim');
        if($keyword){
            $where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
        }

        // 模型 和 查询
        $page  = $this->page;
        $rows  = $this->rows;
        $model = new Admin();
        if($where){
            $result = $model->listPage($where, $page, $rows, "id DESC");
        } else{
            $result = $model->isCache(true)->listPage($where, $page, $rows, "id DESC");
        }

        // 权限整理
        if(!empty($result['list'])){
            foreach($result['list'] as $key => $item){
                $result['list'][$key]['role']     = $model->makeRoles(json_decode($item['role']), false);
                $result['list'][$key]['password'] = '';
            }
        }

        return $this->success($result);
    }

    // 查找
    public function show()
    {
        //$this->throwIf(empty($this->id),'请指定查询用户！');

        $where = ['id' => $this->id];
        $model = new Admin();
        $info  = $model->isCache(true)->oneFind($where);
        $this->throwIf($info === false, $model->getError());

        // 权限整理
        $info->password = ''; // 方便展示为空
        $info->role     = json_decode($info->role);
        $info->roles    = $model->makeRoles($info->role, true);

        return $this->success($info);
    }

    // 修改密码
    public function password()
    {
        $password = $this->getData('password', '123456', 'trim');
        $model    = new Admin();
        $data     = ['password' => $password,];
        $data     = $model->checkData($data, ['id' => $this->id,]);
        $this->throwIf($data === false, $model->getError());

        $where  = $this->editWhere();
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 获取权限
    public function role()
    {
        $roles = (new Admin())->getRole();

        return $this->success($roles);
    }

    // 启用禁用
    public function status()
    {
        $_value = $this->getData('status', 0, 'intval');
        $this->throwIf($this->id == 1, '第一个管理员不能被禁用！！');
        $this->throwIf(is_null($_value), '是否启用状态必须填写！');
        $this->throwIf(!in_array($_value, [0, 1]), '是否启用状态必须为0或1！');


        return $this->changeField('status', $_value);
    }

    // 删除不能删除超级用户
    protected function delWhere()
    {
        $this->throwIf($this->id == 1, '第一个管理员不能被删除！！');

        return ['id' => $this->id];
    }

    // 列表查询
    protected function listWhere()
    {
        $where   = [];
        $keyword = $this->getData('keyword', null, 'trim');
        if($keyword){
            $where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
        }

        return $where;
    }

    // 添加
    protected function _before_add()
    {
        $data = [
            'nickname' => $this->getData('nickname', null, 'trim'),
            'username' => $this->getData('username', null, 'trim'),
            'password' => $this->getData('password', '', 'trim'),
            'mobile'   => $this->getData('mobile', null, 'trim'),
            'role'     => $this->getData('role', null, 'is_array'),
            'status'   => $this->getData('status', 1, 'intval'),
        ];
        //$this->throwReturn(count($data['role']) != count($data['role'], 1), '权限必须是一维数组！');

        // 检查唯一性
        $model = new Admin();
        $data  = $model->checkUser($data, $this->id);
        $this->throwIf($data === false, $model->getError());

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $data = [
            'username' => $this->getData('username', null, 'trim'),
            'nickname' => $this->getData('nickname', null, 'trim'),
            'password' => $this->getData('password', '', 'trim'),
            'mobile'   => $this->getData('mobile', null, 'trim'),
            'role'     => $this->getData('role', null, 'is_array'),
            'status'   => $this->getData('status', 1),
        ];

        //$this->throwReturn(count($data['role']) != count($data['role'], 1), '权限必须是一维数组！');
        // 检查唯一性
        $model = new Admin();
        $data  = $model->checkUser($data, $this->id);
        $this->throwIf($data === false, $model->getError());

        return $data;
    }

}


